package com.zyy.service.config.db;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

/**
 *@author zhangyangyang
 *@createTime 2023417 18:07
 *@description mybatis配置
 */
@Configuration
@MapperScan(basePackages = {"com.zyy.service.mapper", "com.zyy.security.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryMonitor")
public class LabelMybatisConfig {

    @Autowired
    @Qualifier("labelDataSource") DataSource labelDataSource;

    @Bean(name = "sqlSessionFactoryLabel") public SqlSessionFactory sqlSessionFactoryMonitor() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(labelDataSource);
        bean.setTypeAliasesPackage("com.zyy.service.entity");
        bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        return bean.getObject();
    }

    @Bean public SqlSessionTemplate sqlSessionTemplateMonitor() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryMonitor());
    }
}
